<?php
declare(strict_types=1);

namespace house;


use app\admin\model\BusinessArea;
use app\admin\model\HouseVillage;
use app\admin\model\Region;
use custom\Log;
use think\Exception;

/**
 * 房源上下架，更新小区/商圈/省市区 房源数
 * Class HouseCount3
 * @package house
 */
class HouseCount3 extends HouseCount {

    public static $region_field1 = 'village_id';
    public static $region_field2 = 'business_area_id';
    public static $region_field3 = 'city_code';

    //上架的房源
    public $arr1;
    //下架的房源
    public $arr2;
    //1:上架 0：下架
    public $type;

    public function __construct($type, array $house_ids) {
        try {
            $this->type = $type;
            //上架
            if ($type == 1) return $this->arr1 = $this->onShelf($house_ids);
            //下架
            else return $this->arr2 = $this->offShelf($house_ids);
        } catch (Exception $e) {
            $err = "异常信息:{$e->getMessage()} | 异常所在的代码行:{$e->getLine()} | 异常文件{$e->getFile()} | php 版本 " . PHP_VERSION . "(" . PHP_OS . ")";
            Log::write($err, "HouseCount3_err.log");
        }
    }


    /**
     * 上架
     * @param string $house_ids
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function onShelf(array $house_ids): array {
        $houseVillage = new HouseVillage();
        $businessArea = new BusinessArea();
        $region = new Region();
        $house = db("house")
            ->alias('h')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->where('h.id', 'in', $house_ids)
            ->where('h.is_shelf', 0)
            ->where('ha.type', 'not null')
            ->where('h.is_update', 1)
            ->where('h.deleted', 0)
            ->where('h.village_id', '>', 0)
            ->field('h.city_code,h.id,h.business_area_id,h.village_id,ha.auction_status')
            ->select();
        if ($house) :
            foreach ($house as $k1 => $v1) {
                //待开始&进行中
                if (in_array($v1['auction_status'], [1, 2])) {
                    self::wh2($houseVillage, $v1[self::$region_field1], 'id')->setInc('sale_num');
                    self::wh2($houseVillage, $v1[self::$region_field1], 'id')->setInc('total_num');

                    self::wh2($businessArea, $v1[self::$region_field2], 'id')->setInc('house_number');
                    self::wh2($businessArea, $v1[self::$region_field2], 'id')->setInc('total_number');

                    self::wh2($region, $v1[self::$region_field3], self::$region_field3)->setInc('house_number');
                    self::wh2($region, $v1[self::$region_field3], self::$region_field3)->setInc('total_number');
                }//结束
                elseif (!in_array($v1['auction_status'], [1, 2])) {
                    self::wh2($houseVillage, $v1[self::$region_field1], 'id')->setInc('total_num');

                    self::wh2($businessArea, $v1[self::$region_field2], 'id')->setInc('total_number');

                    self::wh2($region, $v1[self::$region_field3], self::$region_field3)->setInc('total_number');
                }
            }
        endif;
        return $house;
    }

    /**
     * 下架
     * @param string $house_ids
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function offShelf(array $house_ids): array {
        $houseVillage = new HouseVillage();
        $businessArea = new BusinessArea();
        $region = new Region();
        $house = db("house")
            ->alias('h')
            ->leftJoin('house_auction ha', 'ha.id=h.auction_id')
            ->where('h.id', 'in', $house_ids)
            ->where('h.is_shelf', 1)
            ->where('h.deleted', 0)
            ->field('h.city_code,h.id,h.business_area_id,h.village_id,ha.auction_status')
            ->select();
        if ($house) :
            foreach ($house as $k1 => $v1) {
                //待开始&进行中
                if (in_array($v1['auction_status'], [1, 2])) {
                    self::wh2($houseVillage, $v1[self::$region_field1], 'id')->setDec('sale_num');
                    self::wh2($houseVillage, $v1[self::$region_field1], 'id')->setDec('total_num');

                    self::wh2($businessArea, $v1[self::$region_field2], 'id')->setDec('house_number');
                    self::wh2($businessArea, $v1[self::$region_field2], 'id')->setDec('total_number');

                    self::wh2($region, $v1[self::$region_field3], self::$region_field3)->setDec('house_number');
                    self::wh2($region, $v1[self::$region_field3], self::$region_field3)->setDec('total_number');
                }//结束
                elseif (!in_array($v1['auction_status'], [1, 2])) {
                    self::wh2($houseVillage, $v1[self::$region_field1], 'id')->setDec('total_num');

                    self::wh2($businessArea, $v1[self::$region_field2], 'id')->setDec('total_number');

                    self::wh2($region, $v1[self::$region_field3], self::$region_field3)->setDec('total_number');
                }
            }
        endif;
        return $house;
    }

    /**
     * 获取结果
     * @return array
     */
    public function getContent(): array {
        if ($this->type == 1) return $this->arr1;
        else return $this->arr2;
    }

}